package diplom.uabs.dealing.dealing;

import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

import java.text.SimpleDateFormat;
import java.util.Date;


public class MakeGraph extends ActionBarActivity {

    private final String URL_RU = "http://cbr.ru/scripts/XML_daily.asp?date_req=";
    private final String URL_UA = "http://pfsoft.com.ua/service/currency/?date=";
    private final SimpleDateFormat FORMAT_RU = new SimpleDateFormat("dd/MM/yyyy");
    private final SimpleDateFormat FORMAT_UA = new SimpleDateFormat("ddMMyyyy");
    private String dataURL;
    private SimpleDateFormat dateFormat;
    private TextView tv;
    private LoadData loader;
    ProgressDialog pd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_make_graph);
        Intent intent = getIntent();
        getBank(intent.getIntExtra(BankChoice.itemID, -1));
        pd = (ProgressDialog)getLastCustomNonConfigurationInstance();
        if (pd!=null && pd.getProgress()!= 100) {
            pd.show();
        }
        test();
    }

    private void getBank(int id) {
        switch (id) {
            case 0:
                dataURL = URL_RU;
                dateFormat=FORMAT_RU;
                break;
            case 1:
                dataURL = URL_UA;
                dateFormat=FORMAT_UA;
                break;
            default:
                dataURL = URL_RU;
                dateFormat=FORMAT_RU;
        }
        TextView tv = (TextView)findViewById(R.id.testView);
        tv.setText(getResources().getStringArray(R.array.bankList)[id]);
    }

    private void test() {
        Date d = new Date();


    }

    public void updateOnClick(View v) {
        updateData();



    }

    private void updateData() {
        createDialog();
        loader = new LoadData();
        loader.setDialog(pd);
        loader.execute("path to dat a");

    }

    private void createDialog() {
        pd = new ProgressDialog(this);
        pd.setTitle("Title");
        pd.setMessage("Message");
        // меняем стиль на индикатор
        pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        pd.setMax(100);
        pd.setIndeterminate(true);
        pd.setButton(Dialog.BUTTON_POSITIVE, "Cancel", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                loader.setCnacel();
                dialog.dismiss();
                pd=null;
            }
        });
        pd.setCancelable(false);
        pd.show();
    }

    @Override
    public void onPause() {
        super.onPause();
        if(pd != null && pd.isShowing()) {
            pd.dismiss();
        }
    }



    @Override
    public Object onRetainCustomNonConfigurationInstance() {
        return pd;
    }








    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.make_graph, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.showCBActivity) {
            Intent intent = new Intent(this, BankChoice.class);
            intent.putExtra("rechangeCB", true);
            startActivity(intent);
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}
